const AdminBiz = require('../../../../../../comm/biz/admin_biz.js');
const pageHelper = require('../../../../../../helper/page_helper.js');
const cloudHelper = require('../../../../../../helper/cloud_helper.js');
const AdminCarBiz = require('../../../../biz/admin_car_biz.js');
const validate = require('../../../../../../helper/validate.js');
const PublicBiz = require('../../../../../../comm/biz/public_biz.js');
const projectSetting = require('../../../../public/project_setting.js');

Page({
	/**
	 * 页面的初始数据
	 */
	data: {

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		if (!AdminBiz.isAdmin(this)) return;

		wx.setNavigationBarTitle({
			title: projectSetting.CAR_NAME + '-添加',
		});

		this.setData(AdminCarBiz.initFormData());
		this.setData({
			isLoad: true
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () { },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	url: function (e) {
		pageHelper.url(e, this);
	},
	switchModel: function (e) {
		pageHelper.switchModel(this, e);
	},

	bindFormSubmit: async function () {
		if (!AdminBiz.isAdmin(this)) return;

		let data = this.data;
		let forms = this.selectComponent("#cmpt-form").getForms(true);
		data.formForms = forms;
		for(let k = 0;k < (forms || []).length; k ++) {
			if (forms[k].mark == 'seats') {
				let seatInfo = AdminCarBiz.getSeatInfo(forms[k].val)
				data.formSeatCnt = seatInfo.seatCnt
				data.formSeatList = seatInfo.seatList
			}
		}

		data = validate.check(data, AdminCarBiz.CHECK_FORM, this);
		if (!data) return;
		try {
			// 创建
			let result = await cloudHelper.callCloudSumbit('admin/car_insert', data);
			let carId = result.data;

			// 图片
			await cloudHelper.transFormsTempPics(forms, 'car/', carId + '', 'admin/car_update_forms');

			let callback = async function () {
				PublicBiz.removeCacheList('admin-car-list');
				PublicBiz.removeCacheList('car-list');
				wx.navigateBack();
			}
			pageHelper.showSuccToast('添加成功', 2000, callback);
		} catch (err) {
			console.log(err);
		}
	},

	bindMapTap: function (e) {
		AdminCarBiz.selectLocation(this);
	}
})